package com.example.ccj.b.c.custom.image;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;

import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.entity.MediaExtraInfo;
import com.luck.picture.lib.utils.MediaUtils;
import com.luck.picture.lib.utils.PictureFileUtils;

import java.io.File;

public class MLocalMedia extends LocalMedia {
    public static final int IMAGE_TYPE_LOCAL = 2;
    public static final int IMAGE_TYPE_NETWORK = 1;
    public static final int IMAGE_TYPE_URI = 0;






    public Uri uri;//本地图片访问链接
    public String name;//图片本地名称，原始名称
    public String uploadName;//图片上传名称。保存服务器名称
    public String path;//图片全路径，本地路径
    public String urlPath; //网络图片访问路径
    public String type;//图片类型
    public int width;//图片宽度
    public int height;//图片高度
    public long size;//图片文件大小，单位：Bytes
    public int imageType;//图片类型  0:本地图片  1：图片url链接  2:本地图片id

    public int iamgeId;//本地图片的Id

    //这两个属性在图片展示时使用，用来标记
    public boolean nowShow = false;//是否当前显示
    public boolean selected = false;//是否被选中


    @Override
    public String toString() {
        return "MyImage{" +
                "uri=" + uri +
                ", name='" + name + '\'' +
                ", uploadName='" + uploadName + '\'' +
                ", path='" + path + '\'' +
                ", urlPath='" + urlPath + '\'' +
                ", type='" + type + '\'' +
                ", width=" + width +
                ", height=" + height +
                ", size=" + size +
                ", imageType=" + imageType +
                ", iamgeId=" + iamgeId +
                ", nowShow=" + nowShow +
                ", selected=" + selected +
                '}';
    }


    /**
     * 构造网络资源下的LocalMedia
     *
     * @param url      网络url
     * @return
     */
    public static MLocalMedia generateHttpAsLocalMedia(String url) {
        MLocalMedia myImage = new MLocalMedia();
        myImage.setPath(url);
        myImage.imageType = IMAGE_TYPE_NETWORK;
        myImage.setMimeType(MediaUtils.getMimeTypeFromMediaHttpUrl(url));
        return myImage;
    }

    /**
     * 构造网络资源下的LocalMedia
     *
     * @param url      网络url
     * @return
     */
    public static MLocalMedia generateHttpAsLocalMedia(String url, String mimeType) {
        MLocalMedia myImage = new MLocalMedia();
        myImage.setPath(url);
        myImage.setMimeType(mimeType);
        return myImage;
    }

    /**
     * 构造本地资源下的LocalMedia
     *
     * @param context 上下文
     * @param path    本地路径
     * @return
     */
    public static MLocalMedia generateLocalMedia(Context context, String path) {
        MLocalMedia myImage = new MLocalMedia();
        File cameraFile = PictureMimeType.isContent(path) ? new File(PictureFileUtils.getPath(context, Uri.parse(path))) : new File(path);
        myImage.setPath(path);
        myImage.setRealPath(cameraFile.getAbsolutePath());
        myImage.setFileName(cameraFile.getName());
        myImage.setParentFolderName(MediaUtils.generateCameraFolderName(cameraFile.getAbsolutePath()));
        myImage.setMimeType(MediaUtils.getMimeTypeFromMediaUrl(cameraFile.getAbsolutePath()));
        myImage.setSize(cameraFile.length());
        myImage.setDateAddedTime(cameraFile.lastModified() / 1000);
        String realPath = cameraFile.getAbsolutePath();
        if (realPath.contains("Android/data/") || realPath.contains("data/user/")) {
            myImage.setId(System.currentTimeMillis());
            File parentFile = cameraFile.getParentFile();
            myImage.setBucketId(parentFile != null ? parentFile.getName().hashCode() : 0L);
        } else {
            Long[] mediaBucketId = MediaUtils.getPathMediaBucketId(context, myImage.getRealPath());
            myImage.setId(mediaBucketId[0] == 0 ? System.currentTimeMillis() : mediaBucketId[0]);
            myImage.setBucketId(mediaBucketId[1]);
        }
        MediaExtraInfo mediaExtraInfo;
        if (PictureMimeType.isHasVideo(myImage.getMimeType())) {
            mediaExtraInfo = MediaUtils.getVideoSize(context, path);
            myImage.setWidth(mediaExtraInfo.getWidth());
            myImage.setHeight(mediaExtraInfo.getHeight());
            myImage.setDuration(mediaExtraInfo.getDuration());
        } else if (PictureMimeType.isHasAudio(myImage.getMimeType())) {
            mediaExtraInfo = MediaUtils.getAudioSize(context, path);
            myImage.setDuration(mediaExtraInfo.getDuration());
        } else {
            mediaExtraInfo = MediaUtils.getImageSize(context, path);
            myImage.setWidth(mediaExtraInfo.getWidth());
            myImage.setHeight(mediaExtraInfo.getHeight());
        }
        return myImage;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof LocalMedia)) return false;
        LocalMedia media = (LocalMedia) o;
        boolean isCompare = TextUtils.equals(getPath(), media.getPath())
                && TextUtils.equals(getRealPath(), media.getRealPath());
        return isCompare;
    }
}
